package com.gtw.modules.api.controller;

import com.gtw.common.annotation.LoginUserApi;
import com.gtw.common.utils.R;
import com.gtw.dto.TbSalesLogDto;
import com.gtw.modules.api.service.TbOrderService;
import com.gtw.modules.api.service.TbSalesLogService;
import com.gtw.modules.api.service.TbUserService;
import com.gtw.modules.entity.TbOrder;
import com.gtw.modules.entity.TbOutGoos;
import com.gtw.modules.entity.TbSalesLog;
import com.gtw.modules.entity.TbUser;
import com.gtw.modules.sys.service.TbOutGoosService;
import com.gtw.utils.EntityDtoConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * 代理仓库
 * Created by wuye on 2018/3/23.
 */
@RestController
@RequestMapping("/api/proxydepot")
public class TbProxyDepotController {

    @Autowired
    private TbUserService tbUserService;

    @Autowired
    private TbOrderService tbOrderService;

    @Autowired
    private TbOutGoosService tbOutGoosService;

    @Autowired
    private TbSalesLogService tbSalesLogService;


    /**
     * 加载用户仓库信息
     *
     * @param tbUser
     * @return 当前存货量  累积销售量  累积提货量
     */
    @RequestMapping("/foundByUser")
    public R foundByUser(@LoginUserApi TbUser tbUser) {
        //当前存货量
        tbUser.getAgentinventory();
        //累计销售量 代理人为当前用户的已完成订单信息
        List<TbOrder> orderList = tbOrderService.queryByPorxyid(tbUser.getUserId());
        Float totalfee = 0f;
        for (TbOrder order : orderList) {
            totalfee += order.getTotalfee();
        }
        //累计提货量
        List<TbOutGoos> outGoosList = tbOutGoosService.foundByUserId(tbUser.getUserId());
        Integer totalfee2 = 0;
        for (TbOutGoos outgoos : outGoosList) {
            totalfee2 += outgoos.getMoney();
        }
        proxydepotDto dto = new proxydepotDto();
        dto.setDqch(tbUser.getAgentinventory());
        dto.setLjxl(totalfee);
        dto.setLejt(totalfee2);
        return R.ok().put("data", dto);
    }


    /**
     * 加载销量记录列表
     *
     * @return
     */
    @RequestMapping("/queryXiaoLiangList")
    public R queryXiaoLiangList(@LoginUserApi TbUser tbUser) throws InvocationTargetException, IllegalAccessException {
        List<TbSalesLog> tbSalesLogList = tbSalesLogService.findByUid(tbUser.getUserId());
        List<TbSalesLogDto> tbSalesLogDtoList = new ArrayList<>();
        for (int i = 0; i < tbSalesLogList.size(); i++) {
            TbSalesLogDto td = new TbSalesLogDto();
            EntityDtoConverter edc = new EntityDtoConverter();
            td = (TbSalesLogDto) edc.entityConvertDto(tbSalesLogList.get(i), td);
            td.setTbUser(tbUserService.queryById(tbSalesLogList.get(i).getUserid()));
            tbSalesLogDtoList.add(td);
        }
        return R.ok().put("data", tbSalesLogDtoList);
    }


    /**
     * 加载提货记录列表
     *
     * @return
     */
    @RequestMapping("/queryTiHuoList")
    public R queryTiHuoList(@LoginUserApi TbUser tbUser) {
        List<TbOutGoos> outGoosList = tbOutGoosService.foundByUserId(tbUser.getUserId());

        return R.ok().put("data", outGoosList);
    }


    /**
     * 代理累计销量内部扩展类
     */
    class proxyLJXLDTO {

        private TbUser tbUser;

        private TbOrder tbOrder;

        public TbUser getTbUser() {
            return tbUser;
        }

        public void setTbUser(TbUser tbUser) {
            this.tbUser = tbUser;
        }

        public TbOrder getTbOrder() {
            return tbOrder;
        }

        public void setTbOrder(TbOrder tbOrder) {
            this.tbOrder = tbOrder;
        }
    }

    /**
     * 代理货仓扩展内部类
     */
    class proxydepotDto {
        private Integer dqch;

        private Float ljxl;

        private Integer lejt;

        public Integer getDqch() {
            return dqch;
        }

        public void setDqch(Integer dqch) {
            this.dqch = dqch;
        }

        public Float getLjxl() {
            return ljxl;
        }

        public void setLjxl(Float ljxl) {
            this.ljxl = ljxl;
        }

        public Integer getLejt() {
            return lejt;
        }

        public void setLejt(Integer lejt) {
            this.lejt = lejt;
        }
    }
}
